package ay.mq;

import ay.dto.MoodDTO;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import java.util.logging.Logger;
@Component
public class MoodConsumer implements MessageListener {

    private static final String PRAISE_HASH_KEY = "springMVC-learning.mood.id.list.key";

    private Logger logger = Logger.getLogger(String.valueOf(this.getClass()));

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(Message message) {
        try {
            MoodDTO moodDTO = (MoodDTO) message.getObjectProperty("moodDTO");
            redisTemplate.opsForSet().add(PRAISE_HASH_KEY,moodDTO.getId());
            redisTemplate.opsForSet().add(moodDTO.getId(),moodDTO.getUserId());
            logger.info("消费者--->>>用户id："+moodDTO.getUserId()+"给说说id："+moodDTO.getId()+"点赞了.");

        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
